<?php

// +----------------------------------------------------------------------
// | pay-php-sdk
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/pay-php-sdk
// +----------------------------------------------------------------------
// | 项目设计及部分源码参考于 yansongda/pay，在此特别感谢！
// +----------------------------------------------------------------------

namespace App\Libs\Wechat;

use App\Api\Signup\Models\PaymentDetails;
use App\Api\Signup\Models\SignRefund;
use Pay\Exceptions\GatewayException;

use Pay\Gateways\Wechat;

/**
 * 微信企业退款网关
 * Class TransferGateway
 * @package Pay\Gateways\Wechat
 */
class RefundGateway extends Wechat
{

    /**
     * 当前操作类型
     * @return string
     */
    protected function getTradeType()
    {
        return '';
    }

    /**
     * 应用并返回数据
     * @param array $options
     * @return array
     * @throws GatewayException
     */
    public function apply(array $options = [])
    {
        unset($this->config['sign_type']);
        unset($this->config['trade_type']);
        // unset($this->config['notify_url']);
        $this->config = array_merge($this->config, $options);
        $this->config['sign'] = $this->getSign($this->config);
        info('微信企业退款请求参数', [$this->config, $this->userConfig]);
        $parma = $this->post(
            $this->gateway_refund,
            $this->toXml($this->config),
            [
                'ssl_cer' => $this->userConfig->get('ssl_cer', ''),
                'ssl_key' => $this->userConfig->get('ssl_key', ''),
            ]
        );
        info('微信企业退款返回xml', [$parma]);
        $data = [];
        if ($parma) {
            $data = $this->fromXml($parma);
            info('微信企业退款返回 解析', [$data]);
            if (!isset($data['return_code']) || $data['return_code'] !== 'SUCCESS' || $data['result_code'] !== 'SUCCESS') {
                $error = 'GetResultError:' . $data['return_msg'];
                $error .= isset($data['err_code_des']) ? ' - ' . $data['err_code_des'] : '';
            }
            if (isset($error)) {
                $data['res_error'] = true;
            }
        } else {
            $trade_no = $options['out_refund_no'];
            $pay = PaymentDetails::where('trade_no', $trade_no)->first();
            $source_id =  data_get($pay, 'source_id');
            $source_type =  data_get($pay, 'source_type');

            $upd['reason'] = '微信返回错误，稍后再试';
            $upd['status'] = -100;
            $pay->update($upd);

            if ($source_type == 1) {
                $ref['status'] = 100;
                // 更改退费表为待审核
                SignRefund::where('id', $source_id)->where('source_type', $source_type)->update($ref);
            }
            throw new \LogicException('操作异常，稍后重试', 403);
        }
        return $data;
    }
    /**
     * 查询订单状态
     * @param string $out_trade_no
     * @return array
     * @throws GatewayException
     */
    public function queryOrder($out_trade_no = '')
    {
        $this->config['out_trade_no'] = $out_trade_no;
        $this->unsetTradeTypeAndNotifyUrl();
        return $this->getResult('https://api.mch.weixin.qq.com/pay/refundquery');
    }
}
